<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-table :data="list" stripe>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-table :data="props.row.chapters" stripe border>
              <el-table-column label="章节标题" prop="name"></el-table-column>
              <el-table-column label="视频链接" prop="url"></el-table-column>
              <el-table-column label="排序" prop="sort"></el-table-column>
              <el-table-column
                label="创建时间"
                prop="gmtCreate"
              ></el-table-column>
              <el-table-column label="章节操作" width="200">
                <template slot-scope="scope">
                  <el-button
                    type="info"
                    size="mini"
                    @click="handleUpdate(scope.row.id)"
                    >修改章节</el-button
                  >
                  <el-button
                    type="danger"
                    size="mini"
                    @click="handleDel(scope.row.id)"
                    >删除章节</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column type="index" width="50" label="序号"></el-table-column>
        <el-table-column label="课程标题" prop="title"></el-table-column>
        <el-table-column label="课程描述" prop="description"></el-table-column>
        <el-table-column label="所属分类" prop="courseTypeId"></el-table-column>
        <el-table-column label="创建时间" prop="gmtCreate"></el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="showForm(scope.row.id)"
              >添加章节</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="showBatchForm(scope.row.id)"
              >批量添加</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="保存"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <SaveFrom
        @done="done"
        :courseChapterId="courseChapterId"
        :courseId="courseId"
        :close="dialogVisible"
      />
    </el-dialog>

    <el-dialog
      title="批量保存"
      :visible.sync="dialogVisibleBatch"
      width="40%"
      :before-close="handleCloseBatch"
    >
      <SaveFromBatch
        @done="doneBatch"
        :courseId="courseId"
        :close="dialogVisibleBatch"
      />
    </el-dialog>
  </div>
</template>

<script>
import courseChapterApi from "@/api/courseChapter";
import SaveFrom from "./save";
import SaveFromBatch from './saveBatch'
export default {
  name: "courseChapter",
  components: {
    SaveFrom,
    SaveFromBatch
  },
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      list: [],
      dialogVisible: false,
      courseChapterId: "",
      courseId:"",
      dialogVisibleBatch:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    done() {
      this.dialogVisible = false;
      this.getList();
    },
    doneBatch() {
      this.dialogVisibleBatch = false;
      this.getList();
    },

    close() {
      this.dialogVisible = false;
    },
    handleCloseBatch(){
      this.dialogVisibleBatch = false;
    },

    async getList() {
      let res = await courseChapterApi.getChapters(this.page);
      if (res.success) {
        this.list = res.data.records;
        this.page.total = res.data.total;
      } else {
        return this.$message.error(res.msg);
      }
    },
    handleCurrentChange(newNum) {
      this.page.pageNum = newNum;
      this.getList();
    },
    handleSizeChange(newSize) {
      this.page.newSize = newSize;
      this.getList();
    },
    showForm(courseId) {
      this.courseId = courseId
      this.courseChapterId = "";
      this.dialogVisible = true;
    },
    showBatchForm(courseId) {
      this.courseId = courseId
      this.dialogVisibleBatch = true;
    },
    handleClose() {
      this.courseId = ""
      this.courseChapterId = "";
      this.dialogVisible = false;
    },
    handleUpdate(id) {
      this.courseChapterId = id;
      this.dialogVisible = true;
    },
    async handleDel(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await courseChapterApi.deletes(id);
          if (res.success) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            return this.$message.error(res.msg);
          }
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
</style>